package com.jky.trial.common.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.jky.trial.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;
import static org.apache.xmlgraphics.ps.DSCConstants.TITLE;

/**
 * 极光推送集成类
 * Created by suweiming on 2018/6/19.
 */
//@Component
public class JPushUtils {

    private static Logger logger = LoggerFactory.getLogger(JPushUtils.class);

    /**
     * 发送实体
     * 发送消息
     *
     * @param payload
     */
    public static PushResult send(PushPayload payload) {

        JPushClient jpushClient = new JPushClient(Constants.jgMasterSecret,
                Constants.jgAppKey, null, ClientConfig.getInstance());

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Got result - " + result);
            Thread.sleep(5000);
            jpushClient.close();
            return result;
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);


        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static PushPayload buildPushObjectTo(String regId,String title,String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(regId))
                .setNotification(Notification.android(content, title, null))
                .build();
    }

    /**
     * 发送推送
     * @param regId
     * @param title
     * @param content
     * @return
     */
    public static PushResult sendPush(String regId,String title,String content) {
        return send(buildPushObjectTo(regId,title,content));
    }



    /**
     * 构建机关推送消息体
     *
     * @param content        消息文本内容
     * @param platform       目标平台
     * @param registrationId 极光推送注册ID
     * @return
     */
    public static PushPayload buildPushObject(String content, Platform platform, List<String> registrationId, Map map) {
        return PushPayload.newBuilder()
                .setPlatform(platform)
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.registrationId(registrationId))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .addExtra("from", "JPush")
                        .addExtra("msg", map != null ? JSONObject.toJSONString(map) : "{}")
                        .build())
                .build();
    }

}
